<template>
  <a-drawer width="35%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>{{ formTitle }}</b>
    </a-divider>
    <a-form-model ref="form" :model="form" :rules="rules">
      <a-form-model-item label="姓名" prop="name" >
        <a-input v-model="form.name" placeholder="请输入姓名" />
      </a-form-model-item>
      <a-form-model-item label="手机号" prop="mobile" >
        <a-input v-model="form.mobile" placeholder="请输入手机号" />
      </a-form-model-item>
      <a-form-model-item label="奶品" prop="type" >
        <a-select v-model="form.type" placeholder="请选择奶品" allow-clear >
          <a-select-option v-for="(d, index) in milkTypeOptions" :key="index" :value="d.dictValue" >{{ d.dictLabel }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="配送类型" prop="delivery" >
        <a-select v-model="form.delivery" placeholder="请选择配送类型" allow-clear >
            <a-select-option v-for="(d, index) in dTypeOptions" :key="index" :value="d.dictValue" >{{ d.dictLabel }}</a-select-option>
          </a-select>
      </a-form-model-item>
      <a-form-model-item label="配送类型" prop="delivery" >
        <a-time-picker format="HH" v-model="form.time" valueFormat="HH"/>
      </a-form-model-item>
      <a-form-model-item label="地址" prop="addressCode" >
        <a-select v-model="form.addressCode" placeholder="请输入地址">
          <a-select-option v-for="(d, index) in dictOptions" :key="index" :value="d.dictValue" >{{ d.dictLabel }}</a-select-option>
        </a-select>
        <a-button type="primary" :loading="submitLoading" @click="addAddress">
          新增地址
        </a-button>
      </a-form-model-item>
      <a-form-model-item label="详细地址" prop="address" >
        <a-input v-model="form.address" placeholder="请输入地址" />
      </a-form-model-item>
      
      <a-form-model-item label="备注" prop="remark" >
        <a-input v-model="form.remark" placeholder="请输入内容" type="textarea" allow-clear />
      </a-form-model-item>
      <div class="bottom-control">
        <a-space>
          <a-button type="primary" :loading="submitLoading" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
      </div>
    </a-form-model>
    <a-modal v-model:visible="addressOpen" title="新增地址" @ok="handleOk">
      <a-form-model ref="addressForm" :model="addressForm" >
        <a-form-model-item label="地址名称" >
          <a-input v-model="addressForm.dictLabel" placeholder="请输入地址名称" allow-clear />
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </a-drawer>
</template>

<script>
import { getMilkMember, addMilkMember, updateMilkMember } from '@/api/system/milkMember'
import { getMilkDictOp,saveAddress } from '@/api/system/milkDict'

export default {
  name: 'CreateForm',
  props: {
  },
  components: {
  },
  data () {
    return {
      dTypeOptions:[],
      milkTypeOptions:[],
      addressForm:{
        dictLabel:"",
        dictValue:"",
      },
      addressOpen: false,
      dictOptions:[],
      submitLoading: false,
      formTitle: '',
      // 表单参数
      form: {
        id: null,

        name: null,

        mobile: null,

        address: null,

        addressCode: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        remark: null

      },
      // 1增加,2修改
      formType: 1,
      open: false,
      rules: {
      }
    }
  },
  filters: {
  },
  created () {
    this.getDictList("address")
    this.getMilkType()
    this.getDelivery()
  },
  computed: {
  },
  watch: {
  },
  mounted () {
  },
  methods: {
    getMilkType(){
      getMilkDictOp({code:'milkType'}).then(resp => {
        this.milkTypeOptions = resp
      })
    },
    getDelivery(){
      getMilkDictOp({code:'Delivery'}).then(resp => {
        this.dTypeOptions = resp
      })
    },
    handleOk(){
      var that = this
      this.$confirm({
        title: '确认新增地址?',
        content: '确认新增地址：' + this.addressForm.dictLabel + '?',
        onOk () {
          saveAddress(that.addressForm).then(resp => {
            that.$message.success('新增地址成功！',3)
            that.getDictList("address")
            that.addressOpen = false
          })
        },
        onCancel () {}
      })
    },
    addAddress(){
      this.addressOpen = true
    },
    getDictList(code){
      getMilkDictOp({code:code}).then(resp => {
        this.dictOptions = resp
      })
    },
    onClose () {
      this.open = false
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.formType = 1
      this.form = {
        id: null,

        name: null,

        mobile: null,

        address: null,

        addressCode: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        remark: null

      }
    },
    /** 新增按钮操作 */
    handleAdd (row) {
      this.reset()
      this.formType = 1
      this.open = true
      this.formTitle = '添加'
    },
    /** 修改按钮操作 */
    handleUpdate (row, ids) {
      this.reset()
      this.formType = 2
      const id = row ? row.id : ids
      getMilkMember(id).then(response => {
        this.form = response.data
        this.open = true
        this.formTitle = '修改'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id !== undefined && this.form.id !== null) {
            updateMilkMember(this.form).then(response => {
              this.$message.success(
                '修改成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            addMilkMember(this.form).then(response => {
              this.$message.success(
                '新增成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
